package com.xdemo.personal.async;

import com.xdemo.manager.IAsyncWork;
import com.xdemo.manager.IMemcacheManager;
import com.xdemo.personal.model.Player;
import com.xdemo.personal.server.pool.EManager;
import com.xdemo.personal.server.pool.ManagerPool;
import com.xdemo.personal.socketserver.msg.ResultFoundMessage;

public class QueryWork implements IAsyncWork {

	private final int queryKey;
	
	private volatile long answer;
	
	private volatile boolean found;
	
	private Player player;
	
	public QueryWork(Player player,int queryKey) {
		this.player = player;
		this.queryKey = queryKey;
	}
	
	@Override
	public void preAction() {
		
	}

	@Override
	public void doAction() {
		IMemcacheManager _manager = (IMemcacheManager)ManagerPool.getManager(EManager.Memcache);
		Long _result = (Long)_manager.get("object" + queryKey);
		if(_result != null) {
			answer = _result.longValue();
			found = true;
		}
	}

	@Override
	public void afterAction() {
		if(found) {
			player.sendMessage(new ResultFoundMessage(queryKey,answer));
		}
	}

}
